<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #cate_id="{ record }">
            <!-- 对标题加上 tag -->
            <div>{{ record.cate_name }}</div>
      </template>
      <template #user_id="{ record }">
            <!-- 对标题加上 tag -->
            <div>{{ record.user.username }}</div>
      </template>
      <template #sort="{ record }">
            <!-- 对标题加上 tag -->
            <a-input-number :style="{width:'200px'}" @change="handleChange($event, record.id)" v-model="record.sort" placeholder="Please Enter" :default-value="500" mode="button" class="input-demo" />
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import oaProcess from '@/api/oa/oaProcess'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()
const cate_id = ref()
const inforow = ref()

const open = (row) => {
  cate_id.value = row.id
  inforow.value = row
  crudRef.value.requestData()
}


const numberOperation = (newValue, id, numberName) => {
  oaProcess.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  oaProcess.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const handleChange = async (value, id) => {
    const response = await oaProcess.numberOperation({ id, numberName: 'sort', numberValue: value })
    if (response.success) {
      Message.success(response.message)
    }
  }

const options = reactive({
  id: 'flyos_oa_process',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  bordered: {cell:true},
  showTools:false,
  showIndex:false,
  expandSearch:false,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: oaProcess.getList,
  recycleApi: oaProcess.getRecycleList,
  add: {
    show: true,
    api: oaProcess.save,
    auth: ['oa:process:save']
  },
  edit: {
    show: true,
    api: oaProcess.update,
    auth: ['oa:process:update']
  },
  delete: {
    show: true,
    api: oaProcess.deletes,
    auth: ['oa:process:delete'],
    realApi: oaProcess.realDeletes,
    realAuth: ['oa:process:realDeletes']
  },
  recovery: {
    show: true,
    api: oaProcess.recoverys,
    auth: ['oa:process:recovery']
  },
  import: {
    show: false,
    url: 'oa/process/import',
    templateUrl: 'oa/process/downloadTemplate',
    auth: ['oa:process:import']
  },
  export: {
    show: true,
    url: 'oa/process/export',
    auth: ['oa:process:export']
  },
  beforeRequest: (params) => {
      params.cate_id = cate_id.value
      params.order = 'created_at'
      params.orderType = 'desc'
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    bordered: {cell:true},
    showTools:false,
    showIndex:false,
    expandSearch:false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入ID"
    }
  },
  // {
  //   title: "分类ID",
  //   dataIndex: "cate_id",
  //   formType: "select",
  //   search: true,
  // },
  {
    title: "分类",
    dataIndex: "cate_id",
    formType: "select",
    // multiple: true,
    search: true,
    dict: {
        // 远程通用接口请求，新版代码生成都有一个 remote 接口
        remote: 'oa/cate/remote',
        // 指定组件接收的props
        props: { label: 'name', value: 'id' },
        // 远程请求配置项
        remoteOption: {
            // 设置查询的字段
            select: [ 'id', 'name' ],
            // 设置数据过滤
            filter: {
            },
            // 关联模型
            relations: [
                // 其他关联....其他可查看 MapperTiart.php 的源代码自行参考
            ]
        }
    }
  },
  {
    title: "所属客户",
    dataIndex: "user_id",
    formType: "select",
    // multiple: true,
    search: true,
    dict: {
        // 远程通用接口请求，新版代码生成都有一个 remote 接口
        remote: 'system/user/remote',
        // 指定组件接收的props
        props: { label: 'username', value: 'id' },
        // 远程请求配置项
        remoteOption: {
            // 设置查询的字段
            select: [ 'id', 'username' ],
            // 设置数据过滤
            filter: {
            },
            // 关联模型
            relations: [
                // 其他关联....其他可查看 MapperTiart.php 的源代码自行参考
            ]
        }
    }
  },
  {
    title: "排序",
    dataIndex: "sort",
    formType: "input",
    width:'150',
    search: true
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "修改时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
defineExpose({ open })
</script>
<script> export default { name: 'oa:process' } </script>